package ui;

import java.util.List;
import java.util.Random;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import com.planesnet.task1.R;
import com.planesnet.task1.Task;
import com.planesnet.task1.R.layout;

import android.view.View;
import android.widget.ArrayAdapter;

public class MainTaskActivity extends ListActivity {
	private Task task = null;
	private ArrayAdapter<Task> adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

	}

	public void onClickAdd(View view) {

		Intent intent = new Intent(this, TaskDetailActivity.class);
		startActivity(intent);
	}

	public void onClickDelete(View view) {
		@SuppressWarnings("unchecked")
		ArrayAdapter<Task> adapter = (ArrayAdapter<Task>) getListAdapter();
		if (getListAdapter().getCount() > 0) {
			task = (Task) getListAdapter().getItem(0);
			adapter.remove(task);
			task.delete();
		}
		adapter.notifyDataSetChanged();
	}



	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	protected void onResume() {
		Task.setContext(this);
		task = new Task();


		List<Task> values = task.getAll();

		// Use the SimpleCursorAdapter to show the
		// elements in a ListView
		adapter = new ArrayAdapter<Task>(this,
				android.R.layout.simple_list_item_1, values);
		setListAdapter(adapter);

		super.onResume();
	}

	@Override
	protected void onPause() {
		// task.close();
		super.onPause();
	}

}